package com.ogawa.projectcommon.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.ogawa.base.base.BaseApp;
import com.ogawa.base.utils.HexUtil;
import com.ogawa.base.utils.ScreenUtils;
import com.ogawa.projectcommon.R;
import com.ogawa.projectcommon.bean.GasBean;
import com.ogawa.projectcommon.bean.HeatRollBean;
import com.ogawa.projectcommon.bean.MovementBean;

import java.util.ArrayList;
import java.util.List;

public class DIYChartWidget2 extends FrameLayout {
    private int splitTime = 15;
    private int splitColumnCount = 4;
    private Paint mRowLinePaint;  //横线、竖线的画笔
    private Paint mColLinePaint;  //横线、竖线的画笔
    private Paint mAxisTextPaint;  //轴线文字
    private Paint mBeansPaint;  //柱子的绘制
    private Paint mBeansTextPaint;  //柱子的文字绘制
    private int mAxisTextSize = 12;
    private int curTime = 0;
    private int timeSelectColor = Color.parseColor("#3FC5FD");
    private int timeUnSelectColor = Color.parseColor("#181818");
    private final List<MovementBean> movementBeans = new ArrayList<>();
    private final List<GasBean> gasBeans = new ArrayList<>();
    private final List<HeatRollBean> heatBeans = new ArrayList<>();
    private final List<HeatRollBean> rollBeans = new ArrayList<>();
    private GestureDetector gestureDetector;
    private float offsetY;
    private final int radius = ScreenUtils.dip2px(BaseApp.appContext, 7);
    private boolean isTouchable = true;
    private OnAreaClickListener areaClickListener;

    private int tabWidth;

    private int tabStartX;
    private int tabStartY;

    Bitmap closeBitmap;
    int tabLeftRightPadding = 0;
    int tabTopBottomPadding = 0;
    int startAxisTextX = 0;
    int beanPaddingLeftRight = 0;
    int beanPaddingTopBottom = 0;
    int tabSplitHeight;
    private final Path path = new Path();
    private final List<RectF> rects = new ArrayList<>();
    private final RectF closeRect = new RectF();
    private RectF selectRect;
    private int closeIndex;
    private final String[] movementCommand = new String[15];
    private final String[] gasCommand = new String[15];
    private final String[] heatRollCommand = new String[15];
    private byte[] byte1;
    private byte[] byte2;
    private byte[] byte3;
    private byte[] byte4;

    public DIYChartWidget2(Context context) {
        super(context);
        init(null);
    }

    public DIYChartWidget2(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public DIYChartWidget2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public void setDiyData(List<MovementBean> movements
            , List<GasBean> gases
            , List<HeatRollBean> heats
            , List<HeatRollBean> rolls) {
        movementBeans.clear();
        gasBeans.clear();
        heatBeans.clear();
        rollBeans.clear();
        movementBeans.addAll(movements);
        gasBeans.addAll(gases);
        heatBeans.addAll(heats);
        rollBeans.addAll(rolls);
        postInvalidate();
    }

    public void addMovementBean(MovementBean bean) {
        movementBeans.add(bean);
        postInvalidate();
    }

    public void addGasBean(GasBean bean) {
        gasBeans.add(bean);
        postInvalidate();
    }

    public void addHeatBean(HeatRollBean bean) {
        heatBeans.add(bean);
        postInvalidate();
    }

    public void addRollBean(HeatRollBean bean) {
        rollBeans.add(bean);
        postInvalidate();
    }

    public List<MovementBean> getMovementBeans() {
        return movementBeans;
    }

    public List<GasBean> getGasBeans() {
        return gasBeans;
    }

    public List<HeatRollBean> getHeatBeans() {
        return heatBeans;
    }

    public List<HeatRollBean> getRollBeans() {
        return rollBeans;
    }

    public void setAreaClickListener(OnAreaClickListener areaClickListener) {
        this.areaClickListener = areaClickListener;
    }

    private void init(AttributeSet attrs) {

        closeBitmap = BitmapFactory.decodeResource(getContext().getResources(),
                R.drawable.img_diy_chart_close);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        tabLeftRightPadding = ScreenUtils.dip2px(getContext(), 20);
        tabTopBottomPadding = ScreenUtils.dip2px(getContext(), 10);
        startAxisTextX = ScreenUtils.dip2px(getContext(), 10);
        beanPaddingLeftRight = ScreenUtils.dip2px(getContext(), 2);
        beanPaddingTopBottom = ScreenUtils.dip2px(getContext(), 0);
        tabSplitHeight = ScreenUtils.dip2px(getContext(), 30);

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.diy_chart_widget);
        if (attrs != null) {
            try {
                mAxisTextSize = a.getDimensionPixelSize(R.styleable.diy_chart_widget_axisTextSize,
                        mAxisTextSize);
                splitColumnCount = a.getInt(R.styleable.diy_chart_widget_splitcolumnCount,
                        splitColumnCount);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                {
                    a.recycle();
                }
            }
        }

        setBackgroundColor(Color.WHITE);
        mRowLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRowLinePaint.setStrokeWidth(ScreenUtils.dip2px(getContext(), 1));
        mRowLinePaint.setColor(Color.parseColor("#229F9F9F"));
        mRowLinePaint.setPathEffect(new DashPathEffect(new float[]{15, 15}, 0));

        mColLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mColLinePaint.setStrokeWidth(ScreenUtils.dip2px(getContext(), 1));
        mColLinePaint.setColor(Color.parseColor("#229F9F9F"));
        mColLinePaint.setPathEffect(new DashPathEffect(new float[]{15, 15}, 0));

        mAxisTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAxisTextPaint.setTextSize(ScreenUtils.sp2px(getContext(), mAxisTextSize));
        mAxisTextPaint.setColor(Color.parseColor("#181818"));
        mAxisTextPaint.setTextAlign(Paint.Align.CENTER);

        mBeansPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBeansPaint.setColor(Color.parseColor("#181818"));
        mBeansPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mBeansPaint.setStrokeJoin(Paint.Join.ROUND);

        mBeansTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBeansTextPaint.setColor(Color.WHITE);
        mBeansTextPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
        mBeansTextPaint.setFakeBoldText(true);
        mBeansTextPaint.setTextAlign(Paint.Align.CENTER);
        gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                offsetY -= distanceY;
                int totalHeight = tabSplitHeight * splitTime;
                float bottomOffset = totalHeight - (getHeight() - 2 * tabTopBottomPadding);
                offsetY = Math.min(offsetY, 0);
                offsetY = Math.max(offsetY, -bottomOffset);
                postInvalidate();
                return super.onScroll(e1, e2, distanceX, distanceY);
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                if (!isTouchable) {
                    return false;
                }
                float x = e.getX();
                float y = e.getY() - offsetY;
                whichCircle(x, y);
                return super.onSingleTapConfirmed(e);
            }

            @Override
            public void onLongPress(MotionEvent e) {
                if (!isTouchable) {
                    return;
                }
                float x = e.getX();
                float y = e.getY() - offsetY;
                closeIndex = getCheckIndex(x, y, true);
                if (closeIndex != -1) {
                    postInvalidate();
                }
                super.onLongPress(e);
            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        tabWidth = getWidth() - tabLeftRightPadding * 2 - getPaddingLeft() - getPaddingRight();
        tabStartX = tabLeftRightPadding + getPaddingLeft();
        tabStartY = tabTopBottomPadding + getPaddingTop();
    }

    public int getCurTime() {
        return curTime;
    }

    public void setCurTime(int curTime) {
        if (this.curTime != curTime) {
            this.curTime = curTime;
            if (selectRect != null) {
                selectRect = null;
            }
            postInvalidate();
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        rects.clear();
        canvas.translate(0, offsetY);
        drawTab(canvas);
        drawBeans(canvas);
    }

    public boolean isTouchable() {
        return isTouchable;
    }

    public void setTouchable(boolean touchable) {
        isTouchable = touchable;
    }

    /**
     * 如果View想处理事件，则view设置为clickable 或 复写 onTouchEvent(),return true;
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }


    /**
     * 消费点击事件    根据点击事件确认是否点击了删除
     *
     * @param x
     * @param y
     */
    private void whichCircle(float x, float y) {
        if (closeRect.contains(x, y)) {//点击的是删除
            switch (getListIndex(closeIndex)) {
                case 0:
                    movementBeans.remove(getAreaListIndex(closeIndex, 0));
                    break;
                case 1:
                    gasBeans.remove(getAreaListIndex(closeIndex, 1));
                    break;
                case 2:
                    heatBeans.remove(getAreaListIndex(closeIndex, 2));
                    break;
                case 3:
                    rollBeans.remove(getAreaListIndex(closeIndex, 3));
                    break;
            }
            closeRect.set(0, 0, 0, 0);
            selectRect = null;
            postInvalidate();
        } else {//点击色块
            if (selectRect != null) {
                selectRect = null;
                postInvalidate();
            }
            int clickIndex = getCheckIndex(x, y, false);
            if (clickIndex != -1) {
                int listIndex = getListIndex(clickIndex);
                int areaListIndex = getAreaListIndex(clickIndex, listIndex);
                if (areaClickListener != null) {
                    areaClickListener.onAreaClick(listIndex, areaListIndex);
                }
            }
        }
    }

    /**
     * 获取色块在对应集合中的下标
     *
     * @param clickIndex
     * @param listIndex
     * @return
     */
    private int getAreaListIndex(int clickIndex, int listIndex) {
        int areaListIndex = -1;
        switch (listIndex) {
            case 0:
                areaListIndex = clickIndex;
                break;
            case 1:
                areaListIndex = clickIndex - movementBeans.size();
                break;
            case 2:
                areaListIndex = clickIndex - movementBeans.size() - gasBeans.size();
                break;
            case 3:
                areaListIndex = clickIndex - movementBeans.size() - gasBeans.size() - heatBeans.size();
                break;
        }
        return areaListIndex;
    }

    /**
     * 确定点击区域的矩形下标
     *
     * @param x
     * @param y
     * @return
     */
    private int getCheckIndex(float x, float y, boolean isLongClick) {
        int checkIndex = -1;
        for (RectF rect : rects) {
            if (rect.contains(x, y)) {
                if (isLongClick) {//长按时才记录选中的rect
                    selectRect = rect;
                }
                checkIndex = rects.indexOf(rect);
                break;
            }
        }
        return checkIndex;
    }

    /**
     * 确定点击的区域属于哪一个集合
     *
     * @param index
     * @return
     */
    private int getListIndex(int index) {
        if (index < movementBeans.size()) {//机芯的数据
            return 0;
        } else if (index < movementBeans.size() + gasBeans.size()) {//气囊的数据
            return 1;
        } else if (index < movementBeans.size() + gasBeans.size() + heatBeans.size()) {//加热的数据
            return 2;
        } else if (index < movementBeans.size() + gasBeans.size() + heatBeans.size() + rollBeans.size()) {//脚滚的数据
            return 3;
        }
        return -1;
    }

    private void drawBeans(Canvas canvas) {
        for (MovementBean movementBean : movementBeans) {
            StringBuilder text = new StringBuilder();
            switch (movementBean.getSideType()) {
                case 0://颈部
                    text.append(getContext().getString(R.string.neck));
                    break;
                case 1://肩部
                    text.append(getContext().getString(R.string.shoulder));
                    break;
                case 2://上背
                    text.append(getContext().getString(R.string.up_back));
                    break;
                case 3://下背
                    text.append(getContext().getString(R.string.down_back));
                    break;
                case 4://腰部
                    text.append(getContext().getString(R.string.waist));
                    break;
                case 5://臀部
                    text.append(getContext().getString(R.string.hip));
                    break;
            }
            text.append("\n[");
            switch (movementBean.getHandType()) {
                case 0://揉捏
                    text.append(getContext().getString(R.string.knead));
                    break;
                case 1://敲击
                    text.append(getContext().getString(R.string.knock));
                    break;
                case 2://拍打
                    text.append(getContext().getString(R.string.clap));
                    break;
                case 3://瑞典式
                    text.append(getContext().getString(R.string.swedish));
                    break;
                case 4://指压
                    text.append(getContext().getString(R.string.zhiya));
                    break;
                case 5://推拿
                    text.append(getContext().getString(R.string.tuina));
                    break;
            }
            text.append("]");
            drawArea(canvas, 0, text.toString(), "#CC8EE2CC"
                    , movementBean.getStartTime(), movementBean.getEndTime());
        }
        for (GasBean gasBean : gasBeans) {
            drawArea(canvas, 1, getContext().getString(R.string.leg_gas), "#CCFF978A"
                    , gasBean.getStartTime(), gasBean.getEndTime());
        }
        for (HeatRollBean heatBean : heatBeans) {
            drawArea(canvas, 2, getContext().getString(R.string.back_heat), "#CC7C99FE"
                    , heatBean.getStartTime(), heatBean.getEndTime());
        }
        for (HeatRollBean rollBean : rollBeans) {
            drawArea(canvas, 3, getContext().getString(R.string.foot_roll), "#CCFEC767"
                    , rollBean.getStartTime(), rollBean.getEndTime());
        }
        if (selectRect != null) {
            int bitWidth = closeBitmap.getWidth();
            closeRect.left = selectRect.right - bitWidth * (2 / 3f);
            closeRect.top = selectRect.top - bitWidth / 4f;
            closeRect.right = closeRect.left + bitWidth;
            closeRect.bottom = closeRect.top + bitWidth;
            canvas.drawBitmap(closeBitmap, closeRect.left, closeRect.top, mBeansPaint);
        }
    }

    /**
     * 绘制区域
     *
     * @param canvas
     * @param columnIndex
     * @param text
     * @param runColor
     * @param startTime
     * @param endTime
     */
    private void drawArea(Canvas canvas, int columnIndex, String text, String runColor, int startTime, int endTime) {
        int splitWidth = tabWidth / splitColumnCount;
        int left = tabStartX + columnIndex * splitWidth + beanPaddingLeftRight;
        int right = left + splitWidth - beanPaddingLeftRight * 2;
        int top = tabStartY + startTime * tabSplitHeight + beanPaddingTopBottom;
        int bottom = top + tabSplitHeight * (endTime - startTime) - beanPaddingTopBottom * 2;
        rects.add(new RectF(left, top, right, bottom));

        if (curTime <= endTime && curTime > startTime) {
            mBeansPaint.setColor(Color.parseColor(runColor));
        } else {
            mBeansPaint.setColor(Color.parseColor("#CCE2E2E2"));
        }
        path.reset();
        int quadH = ScreenUtils.dip2px(getContext(), 7);
        path.moveTo(left, top + radius);
        path.addArc(left, top, left + radius, top + radius, -180f, 90f);
        path.quadTo(left + (right - left) / 2f, top + quadH, right - radius, top);
        path.arcTo(right - radius, top, right, top + radius, -90f, 90f, false);
        path.lineTo(right, bottom);
        path.arcTo(right - radius, bottom - radius, right, bottom, 0, 90f, false);
        path.quadTo(left + (right - left) / 2f, bottom + quadH, left, bottom);
        path.arcTo(left, bottom - radius, left + radius, bottom, 90, 90f, false);
        path.close();
        canvas.drawPath(path, mBeansPaint);

        int beanTextPadding = ScreenUtils.dip2px(getContext(), 3);
        int tabItemWidth = right - left - beanTextPadding * 2;
        int tabItemHeight = bottom - top;

        TextPaint textPaint = new TextPaint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(ScreenUtils.sp2px(getContext(), 11));
        textPaint.setAntiAlias(true);
        StaticLayout layoutOpen = new StaticLayout(text,
                textPaint, tabItemWidth, Layout.Alignment.ALIGN_CENTER,
                1.0F, 0.0F, true);
        canvas.save();
        Rect mTextBound = new Rect();
        mBeansTextPaint.getTextBounds(text, 0, text.length(), mTextBound);
        canvas.translate(left + beanTextPadding,
                top + tabItemHeight / 2f - mTextBound.height() * 2 - beanTextPadding);
        layoutOpen.draw(canvas);
        canvas.restore();
    }

    /**
     * 绘制虚线
     *
     * @param canvas
     */
    private void drawTab(Canvas canvas) {

        int tabWidth = getMeasuredWidth() - tabLeftRightPadding * 2 - getPaddingLeft() - getPaddingRight();
        int tabHeight = tabSplitHeight * splitTime;

        int tabStartX = tabLeftRightPadding + getPaddingLeft();
        int tabStartY = tabTopBottomPadding;
        int endTabX = tabStartX + tabWidth;
        int endTabY = tabStartY + tabHeight;

        int tabSplitWidth = tabWidth / splitColumnCount;
        for (int i = 0; i < splitTime; i++) {
            //绘制横线
            int y = (i) * tabSplitHeight + tabStartY;
            if (i != 0) {
                canvas.drawLine(tabStartX, y, endTabX, y, mRowLinePaint);
            }


            if (curTime == i + 1) {
                mAxisTextPaint.setColor(timeSelectColor);
            } else {
                mAxisTextPaint.setColor(timeUnSelectColor);
            }
            String text = "" + (i + 1) + "‘";
            Rect textRect = new Rect();
            mAxisTextPaint.getTextBounds(text, 0, text.length(), textRect);
            int textY = y + tabSplitHeight / 2 + textRect.height() / 2;

            canvas.drawText(text,
                    getPaddingStart() + startAxisTextX, textY, mAxisTextPaint);

        }

        //绘制竖线
        for (int i = 0; i < splitColumnCount - 1; i++) {
            int x = tabStartX + (i + 1) * tabSplitWidth;
            canvas.drawLine(x, tabStartY, x, endTabY, mColLinePaint);
        }
    }

    private void initArrayData() {
        for (int i = 0; i < movementCommand.length; i++) {
            movementCommand[i] = "00000000";
            gasCommand[i] = "000000";
            heatRollCommand[i] = "00";
        }
    }

    /**
     * 计算指令值
     */
    private void calculateData() {
        initArrayData();
        calculateMovement();
        calculateGas();
        calculateHeatAndRoll();
    }

    private void calculateHeatAndRoll() {
        List<byte[]> list = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            list.add(new byte[8]);
        }
        for (HeatRollBean heatBean : heatBeans) {
            for (int i = heatBean.getStartTime(); i < heatBean.getEndTime(); i++) {
                list.get(i)[6] = 1;
            }
        }
        for (HeatRollBean rollBean : rollBeans) {
            for (int i = rollBean.getStartTime(); i < rollBean.getEndTime(); i++) {
                list.get(i)[7] = 1;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            heatRollCommand[i] = HexUtil.bitToHex(list.get(i));
        }
    }

    private void calculateGas() {
        for (GasBean gasBean : gasBeans) {
            byte1 = new byte[8];
            byte2 = new byte[8];
            byte3 = new byte[8];
            for (int i = gasBean.getStartTime(); i < gasBean.getEndTime(); i++) {
                byte1[7] = 1;
                byte1[6] = 1;
                byte1[5] = 1;
                switch (gasBean.getShoulderRank()) {
                    case 1:
                        byte2[7] = 1;
                        break;
                    case 2:
                        byte2[6] = 1;
                        break;
                    case 3:
                        byte2[7] = 1;
                        byte2[6] = 1;
                        break;
                    case 4:
                        byte2[5] = 1;
                        break;
                    case 5:
                        byte2[7] = 1;
                        byte2[5] = 1;
                        break;
                }
                switch (gasBean.getHandRank()) {
                    case 1:
                        byte2[3] = 1;
                        break;
                    case 2:
                        byte2[2] = 1;
                        break;
                    case 3:
                        byte2[3] = 1;
                        byte2[2] = 1;
                        break;
                    case 4:
                        byte2[1] = 1;
                        break;
                    case 5:
                        byte2[2] = 1;
                        byte2[1] = 1;
                        break;
                }
                switch (gasBean.getSeatRank()) {
                    case 1:
                        byte3[7] = 1;
                        break;
                    case 2:
                        byte3[6] = 1;
                        break;
                    case 3:
                        byte3[7] = 1;
                        byte3[6] = 1;
                        break;
                    case 4:
                        byte3[5] = 1;
                        break;
                    case 5:
                        byte3[5] = 1;
                        byte3[7] = 1;
                        break;
                }
                switch (gasBean.getLegRank()) {
                    case 1:
                        byte3[3] = 1;
                        break;
                    case 2:
                        byte3[2] = 1;
                        break;
                    case 3:
                        byte3[3] = 1;
                        byte3[2] = 1;
                        break;
                    case 4:
                        byte3[1] = 1;
                        break;
                    case 5:
                        byte3[2] = 1;
                        byte3[1] = 1;
                        break;
                }
                gasCommand[i] = HexUtil.bitToHex(byte1) + HexUtil.bitToHex(byte2)
                        + HexUtil.bitToHex(byte3);
            }
        }

        byte1 = null;
        byte2 = null;
        byte3 = null;
        byte4 = null;
    }

    private void calculateMovement() {
        for (MovementBean movementBean : movementBeans) {
            byte1 = new byte[8];
            byte2 = new byte[8];
            byte3 = new byte[8];
            byte4 = new byte[8];
            for (int i = movementBean.getStartTime(); i < movementBean.getEndTime(); i++) {
                byte1[7 - movementBean.getSideType()] = 1;
                switch (movementBean.getHandType()) {
                    case 0:
                        byte2[7] = 1;
                        break;
                    case 1:
                        byte2[5] = 1;
                        break;
                    case 2:
                        byte2[3] = 1;
                        break;
                    case 3:
                        byte2[6] = 1;
                        break;
                    case 4:
                        byte2[4] = 1;
                        break;
                    case 5:
                        byte2[2] = 1;
                        break;
                }
                switch (movementBean.getGearDRank()) {//5D强度
                    case 1:
                        byte3[7] = 1;
                        break;
                    case 2:
                        byte3[6] = 1;
                        break;
                    case 3:
                        byte3[6] = 1;
                        byte3[7] = 1;
                        break;
                    case 4:
                        byte3[5] = 1;
                        break;
                    case 5:
                        byte3[7] = 1;
                        byte3[5] = 1;
                        break;
                    case 6:
                        byte3[5] = 1;
                        byte3[6] = 1;
                        break;
                }
                switch (movementBean.getWidthRank()) {
                    case 3:
                        byte3[3] = 1;
                        break;
                    case 2:
                        byte3[2] = 1;
                        break;
                    case 1:
                        byte3[3] = 1;
                        byte3[2] = 1;
                        break;
                }
                switch (movementBean.getStrongRank()) {
                    case 1:
                        byte4[7] = 1;
                        break;
                    case 2:
                        byte4[6] = 1;
                        break;
                    case 3:
                        byte4[7] = 1;
                        byte4[6] = 1;
                        break;
                    case 4:
                        byte4[5] = 1;
                        break;
                    case 5:
                        byte4[7] = 1;
                        byte4[5] = 1;
                        break;
                }
                switch (movementBean.getSpeedRank()) {
                    case 1:
                        byte4[3] = 1;
                        break;
                    case 2:
                        byte4[2] = 1;
                        break;
                    case 3:
                        byte4[3] = 1;
                        byte4[2] = 1;
                        break;
                    case 4:
                        byte4[1] = 1;
                        break;
                    case 5:
                        byte4[3] = 1;
                        byte4[1] = 1;
                        break;
                }
                movementCommand[i] = HexUtil.bitToHex(byte1) + HexUtil.bitToHex(byte2)
                        + HexUtil.bitToHex(byte3) + HexUtil.bitToHex(byte4);
            }
        }
        byte1 = null;
        byte2 = null;
        byte3 = null;
        byte4 = null;
    }

    /**
     * 获取最终指令
     *
     * @param diyIndex
     * @return
     */
    public String getTotalCommand(String diyIndex) {
        calculateData();
        StringBuilder command = new StringBuilder(diyIndex);
        for (int i = 0; i < 15; i++) {
            command.append(movementCommand[i])//机芯指令4字节
                    .append("00")//第5个字节预留
                    .append(gasCommand[i])//充气指令3字节
                    .append("00")//第9个字节预留
                    .append("00")//第10个字节预留
                    .append("00")//第11个字节预留
                    .append("00")//第12个字节预留
                    .append(heatRollCommand[i]);//第十个字节脚滚+加热
        }
        return command.toString();
    }

    public void clearData() {
        movementBeans.clear();
        gasBeans.clear();
        heatBeans.clear();
        rollBeans.clear();
        postInvalidate();
    }

    public interface OnAreaClickListener {
        void onAreaClick(int listIndex, int areaListIndex);
    }
}
